﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TestStepsEditor.Viewers.Data;

namespace TestStepsEditor.Actions
{
    internal class LoadData : InsertImages
    {
        public static SharedData LoadGridViewData(SharedData data)
        {
            //var data.GridView = new DataGridView(); 
            data.WorkItemAttributes = new WorkItemAttributes();
            var sharedStepIndex = new List<int>();


            //Set work item to null
            data.TestWorkItem = null;
            data.StepsCollection = new BindingList<SimpleStep>();

            try
            {
                data.TestWorkItem = data.TestProject.TestCases.Find(data.WorkItemID) ??
                                    (ITestBase) data.TestProject.SharedSteps.Find(data.WorkItemID);
            }
            catch (DeniedOrNotExistException ex)
            {
                MessageBox.Show(@"Could not load work item: " + ex.Message, @"Work Item Load Error");
                return null;
            }
            if (data.TestWorkItem == null)
            {
                MessageBox.Show(@"The number " + data.WorkItemID + @" is not a Test Case or Shared Step.");
                return null;
            }

            data.WorkItemAttributes.Name = data.TestWorkItem.Id + @" - " + data.TestWorkItem.Title;
            data.WorkItemAttributes.Area = data.TestWorkItem.Area;
            data.WorkItemAttributes.Description = data.TestWorkItem.Description;
            data.WorkItemAttributes.IterationPath = data.TestWorkItem.WorkItem.IterationPath;
            data.WorkItemAttributes.Priority = data.TestWorkItem.Priority;
            try
            {
                data.WorkItemAttributes.StartRelease = data.TestWorkItem.WorkItem["Start Release"].ToString();
                data.WorkItemAttributes.EndRelease = data.TestWorkItem.WorkItem["End Release"].ToString();
            }
            catch
            {
            }
            data.GridView.SuspendLayout();
            data.GridView.Enabled = false;


            int testCaseActionNumber = 0;
            foreach (ITestAction action in data.TestWorkItem.Actions)
            {
                var testStep = action as ITestStep;

                if (testStep != null)
                {
                    if (testStep.Attachments.Count > 0)
                        data.StepsCollection.Add(new SimpleStep(null, testStep.Title.ToString(),
                                                                testStep.ExpectedResult.ToString(),
                                                                false, 0, testCaseActionNumber,
                                                                testStep.Attachments));
                    else
                        data.StepsCollection.Add(new SimpleStep(null, testStep.Title.ToString(),
                                                                testStep.ExpectedResult.ToString(),
                                                                false, 0, testCaseActionNumber, null));
                }
                else
                {
                    var sharedStepReference = action as ISharedStepReference;
                    if (sharedStepReference != null)
                    {
                        ISharedStep sharedStep = sharedStepReference.FindSharedStep();
                        if (sharedStep != null)
                        {
                            foreach (ITestAction simpleStep in sharedStep.Actions)
                            {
                                var simpleTestStep = simpleStep as ITestStep;
                                if (simpleTestStep != null)
                                    if (simpleTestStep.Attachments.Count > 0)
                                        data.StepsCollection.Add(new SimpleStep(null, simpleTestStep.Title.ToString(),
                                                                                simpleTestStep.ExpectedResult.ToString(),
                                                                                true,
                                                                                sharedStep.Id, testCaseActionNumber,
                                                                                simpleTestStep.Attachments));
                                    else
                                        data.StepsCollection.Add(new SimpleStep(null, simpleTestStep.Title.ToString(),
                                                                                simpleTestStep.ExpectedResult.ToString(),
                                                                                true,
                                                                                sharedStep.Id, testCaseActionNumber,
                                                                                null));
                            }
                            sharedStepIndex.Add(testCaseActionNumber);
                        }
                    }
                }
                testCaseActionNumber++;
            }
            data.GridView.DataSource = data.StepsCollection;
            data.GridView.Enabled = true;
            data.GridView.ResumeLayout(true);
            if (data.GridView != null)
            {
// ReSharper disable PossibleNullReferenceException
                data.GridView.Columns["SharedStep"].Visible = false;
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
                data.GridView.Columns["GroupByIndexer"].Visible = false;
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
                data.GridView.Columns["SharedStepNumber"].Visible = false;
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
                data.GridView.Columns["Attachment"].Visible = false;
// ReSharper restore PossibleNullReferenceException
                data = AddImageToGrid(data);
                data.GridView.AllowUserToAddRows = false;
                data.GridView.AutoResizeRows();
                data.GridView.AutoResizeColumns();

                if (data.StepsCollection.Count > 0)
                {
                    data.GroupIndexer = data.StepsCollection.Last().GroupByIndexer + 1;
// ReSharper disable PossibleNullReferenceException
                    data.GridView.Columns["image"].HeaderCell.Value = "";
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
                    data.GridView.Columns["ExpectedResult"].HeaderCell.Value = "Expected Result";
// ReSharper restore PossibleNullReferenceException
                    data.GridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
                }
            }
            if (data.TestWorkItem.WorkItem.Type.Name == "Shared Steps")
                MessageBox.Show(
                    @"You are about to edit a Shared Step.  Please Make sure you have approval from the QA Domain Lead");

            return data;
        }

        public static void LoadProperties(SharedData data)
        {
            if (data.TestWorkItem != null)
            {
                var sb = new StringBuilder();
                foreach (Field field in data.TestWorkItem.WorkItem.Fields)
                {
                    if (field.Name != "Local Data Source" & field.Name != "Steps")
                    {
                        sb.AppendLine(field.Name + ": " + field.Value);
                    }
                }
                MessageBox.Show(sb.ToString());
            }
            else
            {
                MessageBox.Show(@"A Work Item must be loaded.");
            }
        }

        public static MainForm NewMainForm()
        {
            MainForm form = new MainForm();
            form.Show();
            return form;
        }
    }
}